/*
 * CS_MARKET_ITEM_DETAIL_INFO.cpp
 *
 *  Created on: 2013. 5. 22.
 *      Author: mega@gamevil.com
 */

#include "../../Command.h"
#include "../../User.h"
#include "../../NLogic/Trade.h"
#include "../../NLogic/Static.h"


namespace ProjectZ { namespace NCommand {

void
Command::CS_MARKET_ITEM_DETAIL_INFO(User * pUser, ::Network::Packet * pPacket)
{
	PREPARE_PACKET;

	uint64_t tradeSeq = 0;
	char classType = 0;
	char sheetType = 0;

	*pPacket >> tradeSeq >> classType >> sheetType;

	NLogic::Trade * pTrade = pUser->GetTrade();
	if (NULL == pTrade)
	{
		DEBUGLOG("CS_MARKET_ITEM_DETAIL_INFO : pTrade is NULL!!");
		return;
	}

	NLogic::Trade::Key clsKey;
	clsKey.tradeSeq		= tradeSeq;
	clsKey.classType	= static_cast<int>(classType);
	clsKey.sheetType	= static_cast<int>(sheetType);

	// 내가 등록한 아이템인 경우.
	if (true == pTrade->IsExistTradeItemData(clsKey))
	{
		NLogic::Trade::TradeItem * pTradeItem = pTrade->GetTradeItemData(clsKey);

		::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
		*pAck << static_cast<int16_t>(ACK_OK);
		*pAck << tradeSeq;
		pTradeItem->WriteToItemPacket(pAck);
		pAck->MakePacket(CMD_SC_MARKET_ITEM_DETAIL_INFO);
		pUser->Send(pAck);
		return;
	}

	// 내가 등록한 아이템이 아닌 경우 Database에서 조회
	NLogic::Trade::TradeItem::Data clsData;
	clsData.item.class_type	= static_cast<int>(classType);
	clsData.item.sub_type	= static_cast<int>(sheetType);

	NLogic::Trade::TradeItem * pTradeItem = NLogic::Static::InstancePtr()->GETTRADEITEMPOOL()->NEW();
	pTradeItem->Open_Normal(tradeSeq, clsData, pUser);

	if (false == pTradeItem->SelectDatabaseTradeItemDetailInfo())
	{
		NLogic::Static::InstancePtr()->GETTRADEITEMPOOL()->DEL(pTradeItem);
		DEBUGLOG("CS_MARKET_ITEM_DETAIL_INFO : false == pTradeItem->SelectDatabaseTradeItemDetailInfo()");
		return;
	}

	::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
	*pAck << static_cast<int16_t>(ACK_OK);
	*pAck << tradeSeq;
	pTradeItem->WriteToItemPacket(pAck);
	pAck->MakePacket(CMD_SC_MARKET_ITEM_DETAIL_INFO);
	pUser->Send(pAck);

	NLogic::Static::InstancePtr()->GETTRADEITEMPOOL()->DEL(pTradeItem);
}

} /*NCommand*/ } /*ProjectZ*/
